if (get_cookie("key")) {
    $.ajax({
        url: ip + "index.php?s=admincms/login/isadmin",
        type: "post",
        data: {
            "user": get_cookie("user"),
            "key": get_cookie("key")
        },
        success: function (_res) {
            var res = JSON.parse(_res);
            if (res.resStatus === 0) {
                // 用户写入页面
                $("#user_name").text(res.data[0].NickName);
            }
            if (res.resStatus === 1) {
                location.href = key_address;
            }

        },
        error: function () {
            location.href = key_address;
        }
    });
} else {
    setTimeout(function(){
        location.href = key_address;
    },0);
}
$(function () {
    var houtai = {
        //防止频繁操作
        old_click_time: 1000,
        click_yes: function () {
            var now_tiem = new Date().getTime();
            if (now_tiem - houtai.old_click_time > 1000) {
                houtai.old_click_time = now_tiem;
                return true;
            } else {
                return false;
            }
        },
        linshi_search: {
            class1: "",
            class2: "",
            state: "",
            search: ""
        },
        linshi_id: "",    // 分类ID
        linshi_caseId:"", //案例ID
        linshi_arrid: [],
        queren_type: "",//0删除
        //提示功能
        hint:function (str) {
            $(".hint").remove();
            $("body").append("<div class='hint'>"+str+"</div>");
            $(".hint").slideDown(300).delay(2000).slideUp();
        },
        jiancha: function () {
            // 判断是够为空
            for (var i = 0; i < $(".add_goods_body .input_is:visible").length; i++) {
                if ($(".add_goods_body .input_is:visible").eq(i).val() === "") {
                    $(".add_goods_body .input_is:visible").eq(i).focus();
                    houtai.hint("请填写 " + $(".add_goods_body .input_is:visible").eq(i).prev().text());
                    return false;
                }
            }
            return true;
        },
        shop_list_ajax: function () {
            $.ajax({
                url: guanwangID+ "index.php?s=admin/Digital/showClass",
                type: "post",
                data: {
                    token:get_cookie("key")
                },
                success: function (_res) {
                    var res = JSON.parse(_res);
                    console.log(res);
                        houtai.table_string(res);

                },
                error:function () {
                    $("#hint_text").text("列表请求失败！！");
                }
            });
        },
        table_string: function (res) {
            // 写入table
            var string_head = "<thead><tr><th>ID</th><th>名称</th><th>介绍 </th ><th>操作</th></tr></thead>";
            var string_body = "";
            if (res.resStatus === 1) {
                $.each(res.data, function (i, o) {
                    string_body += '<tr>' +
                        '<td style="vertical-align: middle" class="fenlei_id text-center">' + o.casusId + '</td>' +
                        '<td style="vertical-align: middle" class="fenlei_name">' + o.casusTitle + '</td>' +
                        '<td style="vertical-align: middle" class="fenlei_jieshao">' + o.explian + '</td>' +
                        '<td style="vertical-align: middle;width: 200px;" class="fenlei_controls text-center"> <button type="button" class="btn btn-warning btn-xs fenlei_set"> <span class="glyphicon glyphicon-tags" aria-hidden="true"></span> 修改</button> <button type="button" class="btn btn-danger btn-xs fenlei_remove"> <span class="glyphicon glyphicon-trash" aria-hidden="true"></span> 删除</button> <button type="button" class="btn btn-info btn-xs anli_chakan"> <span class="glyphicon glyphicon-plus" aria-hidden="true"></span> 查看案例</button></td>' +
                        '</tr>';
                });
            }else{
                string_body = "<tr class='text-center'><td colspan='13'>没有数据</td></tr>";
            }

            $("table").empty().append(string_head + string_body);
        },
        tianjia: function () {
            $.ajax({
                url: guanwangID + "index.php?s=admin/Digital/addClass",
                data: {
                    casusTitle:$.trim($("#add_name").val()),
                    explian:$.trim($("#add_jieshao").val()),
                    token:get_cookie("key")
                },
                type: "post",
                success: function (_res) {
                    var res = JSON.parse(_res);
                    if (res.resStatus === 1) {
                        $("#fenlei_add").fadeOut();
                        houtai.shop_list_ajax();
                        houtai.hint("<p class='text-success'>添加分类成功了！</p>");
                    } else{
                        houtai.hint("添加分类失败了！");
                    }
                },
                error: function () {
                    houtai.hint("<p>添加分类失败了！</p>");
                }
            });

        },
        xiugai: function () {
            $.ajax({
                url:guanwangID + "index.php?s=admin/Digital/editClass",
                data:{
                    casusId:houtai.linshi_id,
                    casusTitle:$.trim($("#set_name").val()),
                    explian:$.trim($("#set_jieshao").val()),
                    token:get_cookie("key")
                },
                type: "post",
                success: function (_res) {
                    var res = JSON.parse(_res);
                    if (res.resStatus === 1) {
                        $("#fenlei_set").fadeOut();
                        houtai.hint("<p class='text-success'>修改成功了！</p>");
                        houtai.shop_list_ajax();
                    } else if (res.resStatus === 1) {
                        houtai.hint("您并没做任何修改啊！");
                    }
                },
                error: function () {
                    houtai.hint("<p>修改失败了！</p>");
                }
            });
        },
        shanchu: function () {
            $.ajax({
                url: guanwangID+ "index.php?s=admin/Digital/delClass",
                type: "post",
                data: {
                    casusId: houtai.linshi_id,
                    token:get_cookie("key")
                },
                success: function (_res) {
                    var res = JSON.parse(_res);
                    if (res.resStatus === 1) {
                        $("#modal_wrap").fadeOut(200);
                        houtai.hint("<p style='color:#0f0;'>删除" + houtai.linshi_id + "成功了</p>");
                        houtai.shop_list_ajax();
                    }
                }
            });
        },
        anli_list_ajax: function () {
            $.ajax({
                url: guanwangID + "index.php?s=admin/Digitaldata/showCasusData",
                type: "post",
                data: {
                    casusId:houtai.linshi_id,
                    token:get_cookie("key")
                },
                success: function (_res) {
                    var res = JSON.parse(_res);
                        houtai.anli_table_string(res);
                },
                error:function () {
                    $("#hint_text").text("列表请求失败！！");
                }
            });
        },
        anli_table_string: function (res) {
            console.log(res);
            // 写入table
            var string_head = "<thead><tr><th>ID</th><th>名称</th><th>上传时间</th ><th>面积</th ><th>价格</th ><th>操作</th></tr></thead>";
            var string_body = "";
            if (res.resStatus === 1) {
                $.each(res.data, function (i, o) {
                    string_body += '<tr>' +
                        '<td style="vertical-align: middle" class="case_id text-center">' + o.dataId + '</td>' +
                        '<td style="vertical-align: middle" class="case_img hidden">' + o.images + '</td>' +
                        '<td style="vertical-align: middle" class="case_content hidden">' + o.dataContent + '</td>' +
                        '<td style="vertical-align: middle" class="case_name">' + o.dataTitle + '</td>' +
                        '<td style="vertical-align: middle" class="case_time">' + o.createTime + '</td>' +
                        '<td style="vertical-align: middle" class="case_area">' + o.scope + '</td>' +
                        '<td style="vertical-align: middle" class="case_price">' + o.price + '</td>' +
                        '<td style="vertical-align: middle;width: 200px;" class="fenlei_controls text-center"> <button type="button" class="btn btn-warning btn-xs case_set"> <span class="glyphicon glyphicon-tags" aria-hidden="true"></span> 修改</button> <button type="button" class="btn btn-danger btn-xs case_remove"> <span class="glyphicon glyphicon-trash" aria-hidden="true"></span> 删除</button></td>' +
                        '</tr>';
                });
            }else{
                string_body = "<tr class='text-center'><td colspan='13'>没有数据</td></tr>";
            }

            $("#anli_table").empty().append(string_head + string_body);
        },
        anli_add:function () {
            var form_data = new FormData();
                form_data.append("images",$("#add_input_file_main")[0].files[0]);
                form_data.append("dataTitle",$.trim($("#add_anli_name").val()));
                form_data.append("myEditor",um_add.getContent());
                form_data.append("scope",$.trim($("#add_anli_area").val()));
                form_data.append("price",$.trim($("#add_anli_price").val()));
                form_data.append("casusId",houtai.linshi_id);
                form_data.append("token",get_cookie("key"));
            $.ajax({
                url: guanwangID + "index.php?s=admin/Digitaldata/addCasusData",
                type: "post",
                data:form_data,
                success: function (_res) {
                    var res = JSON.parse(_res);
                    if (res.resStatus === 1) {
                      $("#anli_add .input_is").val("");
                      $("#add_input_file_main").val("");
                      $(".add_img_wrap_main").empty();
                        um_add.setContent("");
                        houtai.hint("<p style='color:#0f0;'>添加成功了</p>");
                        houtai.anli_list_ajax();
                        $("#anli_add").fadeOut();
                        $(".loading").fadeOut();
                    }else{
                        houtai.hint(res.message);
                        $(".loading").fadeOut();
                    }

                },
                error:function () {
                    $("#hint_text").text("列表请求失败！！");
                },
                processData: false,  // 告诉jQuery不要去处理发送的数据
                contentType: false   // 告诉jQuery不要去设置Content-Type请求头
            });
        },
        anli_set:function () {
            var form_data = new FormData();
            form_data.append("images",$("#set_input_file_main")[0].files[0]);
            form_data.append("dataTitle",$.trim($("#set_anli_name").val()));
            form_data.append("myEditor",um_set.getContent());
            form_data.append("scope",$.trim($("#set_anli_area").val()));
            form_data.append("price",$.trim($("#set_anli_price").val()));
            form_data.append("dataId",houtai.linshi_caseId);
            form_data.append("token",get_cookie("key"));
            $.ajax({
                url: guanwangID + "index.php?s=admin/Digitaldata/editCasusData",
                type: "post",
                data: form_data,
                success: function (_res) {
                    var res = JSON.parse(_res);
                    if (res.resStatus === 1) {
                      $("#anli_set .input_is").val("");
                      $("#set_input_file_main").val("");
                      $(".set_img_wrap_main").empty();
                        um_set.setContent("");
                        houtai.hint("<p style='color:#0f0;'>修改成功了</p>");
                        houtai.anli_list_ajax();
                        $("#anli_set").fadeOut();
                        $(".loading").fadeOut();
                    }else{
                        $(".loading").fadeOut();
                        houtai.hint(res.message);
                    }

                },
                error:function () {
                    $("#hint_text").text("列表请求失败！！");
                },
                processData: false,  // 告诉jQuery不要去处理发送的数据
                contentType: false   // 告诉jQuery不要去设置Content-Type请求头
            });
        },
        anli_remove:function () {
            $.ajax({
                url: guanwangID + "index.php?s=admin/Digitaldata/delCasusData",
                type: "post",
                data: {
                   dataId:houtai.linshi_caseId,
                   token:get_cookie("key")
                },
                success: function (_res) {
                    var res = JSON.parse(_res);
                    if (res.resStatus === 1) {
                        houtai.hint("<p style='color:#0f0;'>删除成功了</p>");
                        houtai.anli_list_ajax();
                        $("#modal_wrap").fadeOut();
                    }else{
                        houtai.hint(res.message);
                    }

                },
                error:function () {
                    $("#hint_text").text("列表请求失败！！");
                }
            });
        },
        huoqu_url_main: function (file) {
            var form_reader = new FileReader();
            form_reader.onload = function (e) {
                $(".add_img_wrap_main").append("<img src='" + e.target.result + "'/>");
            };
            form_reader.readAsDataURL(file);
        },
        huoqu_url_set: function (file) {
            var form_reader = new FileReader();
            form_reader.onload = function (e) {
                $(".set_img_wrap_main").append("<img src='" + e.target.result + "'/>");
            };
            form_reader.readAsDataURL(file);
            console.log(file);
        }
    };

    //实例化编辑器
    var um_add = UM.getEditor('add_myEditor');
    var um_set = UM.getEditor('set_myEditor');
    // 请求数据
    houtai.shop_list_ajax();
    // 收展事件
    $(".right-top>b").bind("click", function () {
        if ($(".content_left").is(":hidden")) {
            $(".content_left").removeClass("ishidden");
            $(".content_right").removeClass("iskuozhang");
        } else {
            $(".content_left").addClass("ishidden");
            $(".content_right").addClass("iskuozhang");
        }
    });

    // 添加分类按钮
    $("#jianshe_add_btn").bind("click", function () {
        $("#fenlei_add").fadeIn();
    });
    // 确认添加分类按钮
    $("#fenlei_add_btn").bind("click",function(){
        if(houtai.jiancha()){
            houtai.tianjia();
        }
    });
    // 莫态框取消modal
    $("#modal_close").bind("click", function () {
        $("#modal_wrap").fadeOut(200);
    });
    // 修改取消
    $(".cancle_set").bind("click", function () {
        $(this).parents(".set_shangpin").fadeOut(200);
    });
    // 分类修改
    $("#fenlei_table").on("click", ".fenlei_set", function () {
        houtai.linshi_id = $(this).parents("tr").find(".fenlei_id").text();
        $("#set_name").val($(this).parents("tr").find(".fenlei_name").text());
        $("#set_jieshao").val($(this).parents("tr").find(".fenlei_jieshao").text());
        $("#fenlei_set").fadeIn();
    });
    // 确认分类修改
    $("#fenlei_set_btn").bind("click",function () {
        if(houtai.jiancha()){
            houtai.xiugai();
        }
    });
    // 案例查看
    $("#fenlei_table").on("click", ".anli_chakan", function () {
        houtai.linshi_id = $(this).parents("tr").find(".fenlei_id").text();
        houtai.anli_list_ajax();
        $("#anli_wrap").fadeIn();
    });
    // 分类删除
    $("#fenlei_table").on("click", ".fenlei_remove", function () {
        houtai.linshi_id = $(this).parents("tr").find(".fenlei_id").text();
        houtai.queren_type=0;
        $(".modal-body").empty().append("您确定要删除<span style='color:#f10;'>" + houtai.linshi_id + "</span>吗？");
        $("#modal_wrap").fadeIn();
    });
    // 确认删除
    $("#confirm").bind("click", function () {
        switch (houtai.queren_type){
            case 0:  houtai.shanchu();
            break;
            case 1:   houtai.anli_remove();
        }
    });
    $("#add").bind("click", function () {
        if (houtai.click_yes()) {
            houtai.tianjia(1);
        }
    });
    $("#set").bind("click", function () {
        if (houtai.click_yes()) {
            houtai.xiugai(2);
        }
    });



    // 案例展示页面
    $("#anli_close").bind("click",function () {
        $("#anli_wrap").hide();
    });
    $("#anli_add_btn").bind("click",function () {
        $("#anli_add").show();
    });
    $("#anli_table").on("click",".case_set",function () {
        houtai.linshi_caseId=$(this).parents("tr").find(".case_id").text();
        um_set.setContent($(this).parents("tr").find(".case_content").text());
        $("#set_anli_name").val($(this).parents("tr").find(".case_name").text());
        $("#set_anli_area").val($(this).parents("tr").find(".case_area").text());
        $("#set_anli_price").val($(this).parents("tr").find(".case_price").text());
        $(".set_img_wrap_main ").empty().append("<img src='"+$(this).parents("tr").find(".case_img").text()+"'/>")
       $("#anli_set").show();
    });
    $("#anli_table").on("click",".case_remove",function () {
        houtai.linshi_caseId=$(this).parents("tr").find(".case_id").text();
        houtai.queren_type=1;
        $(".modal-body").empty().append("您确定要删除 <span style='color: #f10;'>"+$(this).parents("tr").find(".case_name").text()+"</span> 吗？")
        $("#modal_wrap").show();
    });

    // 添加案例页面
     // 添加案例确认按钮
    $("#anli_add_config").bind("click",function () {
        if(houtai.jiancha()){
            if($("#add_input_file_main")[0].files.length === 0){
                houtai.hint("请选择默认上传图片");
                return ;
            }
            if(!um_add.hasContents()){
               houtai.hint("请写入案例内容");
               um_add.focus();
               return;

            }
            $(".loading").show();
            houtai.anli_add();
        }
    });
    // 默认图片上传按钮
    $("#add_up_button_main").bind("click", function () {
        $("#add_input_file_main").click();
    });
    // 默认图片选择时事件
    $("#add_input_file_main").change(function () {
        $(".add_img_wrap_main").empty();
        houtai.huoqu_url_main($(this)[0].files[0]);
    });


    // 案例修改页面
    $("#anli_set_config").bind("click",function () {
       if(houtai.jiancha()){
           if(!um_set.hasContents()){
               houtai.hint("请写入案例内容");
               um_set.focus();
               return;

           }
           $(".loading").show();
           houtai.anli_set();
       }
    });
    // 默认图片上传按钮
    $("#set_up_button_main").bind("click", function () {
        $("#set_input_file_main").click();
    });
    // 默认图片选择时事件
    $("#set_input_file_main").change(function () {
        $(".set_img_wrap_main").empty();
        houtai.huoqu_url_set($(this)[0].files[0]);
    });

    //退出按钮
    $("#exit").bind("click", function () {
        set_cookie("key", "", 0);
        location.href = key_address;
    });
    //头像按钮
    $(".right-top>div").bind("click", function (e) {
        $("#exit").slideToggle();
        e.stopPropagation();
    });
    $(window).bind("click", function () {
        $("#exit").slideUp();
    });

});
